<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="coop.bancocredicoop.proyectos.gd.domain.Document" %>
<%@ page import="coop.bancocredicoop.proyectos.gd.domain.DocumentType" %>
<%@ page import="java.util.List" %>


<%@ include file="../header.jsp" %>

	<link href="${pageContext.request.contextPath}/css/datepicker.css" rel="stylesheet"/>

	<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap-datepicker.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/locales/bootstrap-datepicker.es.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/gd.js"></script>
	<script type="text/javascript">

		var documentTypes;

	    function previewDocument(fileName) {
	        var baseUrl = '${pageContext.request.contextPath}/documento/abrirArchivo';
	        var url = baseUrl + '?fileName=' + fileName;
	        var previewer = GD.DocumentPreviewer();
	        previewer.previewDocument(url, fileName);
	    }

		function useLastCustomer(lastCustomerId, lastCustomerDescription) {
			$('#customerId').val(lastCustomerId);
			$('#customerDescription').val(lastCustomerDescription);
			$('#customer').html(lastCustomerDescription);
		}

		function useLastBox(lastBox) {
			$('#box').val(lastBox);
		}

		function documentTypeSelected() {
			var documentTypeId = $('#documentType').val();
			var docType = documentTypes[documentTypeId];
			$('#purgeable').prop('checked', docType.purgeable);
			setExpirationDate(docType.defaultExpirationDay);
		}

		function setExpirationDate(expirationDate) {
			$('#expirationDate').datepicker('setDate', new Date(expirationDate));
		}

		$(function(){
			$('#expirationDate').datepicker({
				format: 'dd/mm/yyyy',
				todayBtn: 'linked',
				language: 'es'
			});

			var docTypesDataURL = "${pageContext.request.contextPath}/jsp/jsonTiposDocumento/index.jsp?dateFormat=yyyy-MM-dd";
			$.get(docTypesDataURL, function(data) {
				data = jQuery.parseJSON(data.trim());
				documentTypes = data.documentTypes;
				//documentTypeSelected();
				$('#documentType').change(documentTypeSelected);
			});
		});

	</script>

	<div class="container">
	<div class="main-panel">
	<div class="panel panel-default">
	<div class="panel-heading"><h2>Legajo</h2></div>
	<div class="panel-body">

   	<!-- BEGIN: flash messages -->
    <c:if test="${!sessionScope.hasErrors && sessionScope.successMessage != null}">
    	<div class="alert alert-success"><c:out value="${sessionScope.successMessage}" /></div> 
    </c:if>
	<% if (((List<String>) request.getAttribute("listError")) != null &&
			!((List<String>) request.getAttribute("listError")).isEmpty()) { %>
		<% for (String error : ((List<String>) request.getAttribute("listError"))) { %>
			<div class="alert alert-danger"><%=error %></div>
		<% } %> 
	<% } %>
    <% 
    	session.setAttribute("hasErrors", null);
    	session.setAttribute("successMessage", null);
    	session.setAttribute("errorMessages", null);
    %>
    <!-- END: flash messages -->

    <c:if test="${needsNewVersionConfirmation}">
	<div class="alert alert-danger">
		El documento posee una versión anterior, corrija los datos o confirme para versionar
	</div> 
    </c:if>

	<form role="form" method="POST" enctype="multipart/form-data" >

		<input type="hidden" id="documentId" name="documentId" value="<%=request.getAttribute("customerId") != null ? request.getAttribute("documentId") : "" %>" />

		<div class="form-group">
			<label for="inputDocument">Documento</label>
			<input type="file" name="inputDocument" id="inputDocument">
			<p class="help-block" style="color:red"><strong>Para mantener el documento actual NO seleccione ning&uacute;n archivo.</strong></p>
		</div>

		<div class="form-group">
			<label for="customer">Asociado</label>

			<input type="hidden" id="customerId" name="customerId" value="<%=request.getAttribute("customerId") != null ? request.getAttribute("customerId") : "" %>" />
			<input type="hidden" id="customerDescription" name="customerDescription" value="<%=request.getAttribute("customerDescription") != null ? request.getAttribute("customerDescription") : "" %>" />

			<div class="row">
				<div class="col-xs-9">
					<div class="form-control" id="customer">
						<span><%=request.getAttribute("customerDescription") != null ? request.getAttribute("customerDescription") : "" %></span>
					</div>
				</div>

				<div class="col-xs-3">
					<a data-toggle="modal" href="${pageContext.request.contextPath}/jsp/buscarCliente/index.jsp" data-target="#buscarClienteModal" class="btn btn-default">Buscar Asociado</a>
				</div>
			</div>
		</div>

		<div class="form-group">
			<label for="documentType" >Tipo de documento</label>
			<select class="form-control" name="documentType" id="documentType" placeholder="Tipo de documento">
			<% for (DocumentType documentType : (List<DocumentType>) request.getAttribute("listDocumentType")) { %>
				<% if (String.valueOf(documentType.getId()).equals(request.getAttribute("documentType"))) { %>
				<option value="<%=documentType.getId() %>" selected="selected" ><%=documentType.getName() %></option>
				<% } else { %>
				<option value="<%=documentType.getId() %>"><%=documentType.getName() %></option>
				<% } %>  
			<% } %>
			</select>
		</div>

		<fieldset disabled>
			<div class="form-group">
				<label for="purgeable">
					<% if (request.getAttribute("purgeable") != null && request.getAttribute("purgeable").equals("true")) { %>
						<input type="checkbox" id="purgeable" checked="checked" /> Expurgable
					<% } else { %>
						<input type="checkbox" id="purgeable" /> Expurgable
					<% } %>
				</label>
			</div>
		</fieldset>

		<div class="form-group">
			<label for="box">Caja</label>
			<input type="text" class="form-control" name="box" id="box" placeholder="Caja" value="<%=request.getAttribute("box") != null ? request.getAttribute("box") : "" %>">
		</div>

		<div class="form-group">
			<label for="expirationDate">Fecha de vencimiento</label>
			<input type="text" class="form-control" id="expirationDate" name="expirationDate" style="max-width: 12em;" value="<%=request.getAttribute("expirationDate") != null ? request.getAttribute("expirationDate") : "" %>">
		</div>

		<div class="form-group">
			<label for="notes">Notas</label>
			<textarea class="form-control" id="notes" name="notes" placeholder="Notas"><%=request.getAttribute("notes") != null ? request.getAttribute("notes") : "" %></textarea>
		</div>

		<div class="form-group">
			<button type="submit" class="btn btn-default">Aceptar</button>
		</div>
	</form>

	</div>
	</div>
	</div>
	</div>

	<div class="modal fade" id="buscarClienteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>

<%@ include file="../footer.jsp" %>